package com.hzw.saas.api.storage.bo;

import java.io.Serializable;
import java.util.Objects;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author zzl
 * @since 04/24/2021
 */
@Data
@Accessors(chain = true)
@ApiModel("文件Inx分片信息")
public class InxChunkBO implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int CHUNK_NUMBER_START = 1;

    /**
     * 文件ID
     */
    @NotBlank(message = "文件ID")
    @ApiModelProperty(value = "文件ID", position = 1, required = true)
    private String fileId;

    /**
     * 文件名，含后缀
     */
    @ApiModelProperty(value = "文件名", position = 2)
    private String filename;

    /**
     * 总分片数量
     */
    @NotNull(message = "总分片数量不可为空")
    @Min(value = 1, message = "总分片数量不可小于1")
    @ApiModelProperty(value = "总分片数量", position = 3, required = true)
    private Integer totalChunks;

    /**
     * 总大小
     */
    @NotNull(message = "文件总大小不能为空")
    @Min(value = 0, message = "文件总大小不能小于0")
    @ApiModelProperty(value = "整体文件大小", position = 4, required = true)
    private Long totalSize;

    /**
     * 当前为第几块分片
     */
    @NotNull(message = "当前分片次序不可为空")
    @Min(value = 1, message = "当前分片次序不可小于1")
    @ApiModelProperty(value = "当前分片次序，次序从1开始的", position = 5, required = true)
    private Integer chunkNumber;

    /**
     * 每个分片大小
     */
    @NotNull(message = "每个分片大小不可为空")
    @Min(value = 0, message = "每个分片大小不可小于0")
    @ApiModelProperty(value = "每个分片大小", position = 6, required = true)
    private Long chunkSize;

    /**
     * 当前分片大小
     */
    @ApiModelProperty(value = "当前分片大小", position = 7, required = true)
    private Long currentChunkSize;

    /**
     * 文件摘要，md5等
     */
    @ApiModelProperty(value = "分片MD5摘要", position = 8)
    private String digest;

    /**
     * 文件摘要类型
     * 备用
     */
    @ApiModelProperty(value = "摘要类型（可选），默认为MD5，支持MD5,SHA1,SHA256", position = 9)
    private String digestType;

    /**
     * 是否为第一个分片
     *
     * @return true or false
     */
    public boolean onFirst() {
        return Objects.equals(this.getChunkNumber(), CHUNK_NUMBER_START);
    }

    public boolean onLast() {
        return Objects.equals(this.getChunkNumber(), this.getTotalChunks());
    }

}
